//
//  DTDeviceLauncherAction.m
//  PersonalManager
//
//  Created by user on 16/8/16.
//  Copyright © 2016年 Haiyun.Qian. All rights reserved.
//

#import "DTDeviceLauncherAction.h"

@implementation DTDeviceLauncherAction

/** 自定义配合 DTJavascriptCommand 初始化方法 */
- (instancetype)initWithCommand:(DTJavascriptCommand *)command{
    self = [super init];
    self.command = command;
    return self;
}

- (void)checkInstalledApps {
    NSString *appScheme = [self.command.params[@"apps"][0] stringByAppendingString:@"://"];
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appScheme]]) {
        [self mySuccessCallBack];
    }else {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:appScheme, @"无法打开", nil];
        [self.command successRessponse:dic];
    }
}

- (void)launcherApp {
    NSString *appScheme = [self.command.params[@"app"] stringByAppendingString:@"://"];
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appScheme]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appScheme]];
    }else {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:appScheme, @"无法打开", nil];
        [self.command successRessponse:dic];
    }
}

- (void)getNetworkType {
    UIApplication *app = [UIApplication sharedApplication];
    app.statusBarHidden = NO;
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    int type = 0;
    for (id child in children) {
        if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
    }
    NSString *stateString = @"wifi";
    switch (type) {
        case 0:
            stateString = @"none";
            break;
        case 1:
            stateString = @"2G";
            break;
        case 2:
            stateString = @"3G";
            break;
        case 3:
            stateString = @"4G";
            break;
        case 4:
            stateString = @"LTE";
            break;
        case 5:
            stateString = @"wifi";
            break;
            
        default:
            stateString = @"unknown none";
            break;
    }
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:stateString, @"当前网络为:", nil];
    [self.command successRessponse:dic];
    
}

- (void)mySuccessCallBack {
    NSString *appScheme = self.command.params[@"apps"][0];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:appScheme, @"已安装", nil];
    [self.command successRessponse:dic];
}

@end
